//
//  EpointWebInfoCategoryViewController.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-17.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointWebInfoCategoryViewController.h"
#import "DBFrameService.h"
#import "EpointCateGoryInfo.h"
#import "EpointWebInfoListViewController.h"
#import "TDBadgedCell.h"
#import "EpointWebInfoViewControllerCache.h"

@interface EpointWebInfoCategoryViewController ()
{
    UITableView *tv;
    NSMutableArray *dataArray,*flagArr;
    
}
@end

@implementation EpointWebInfoCategoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        flagArr = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    tv = [[UITableView alloc]initWithFrame:[self getFullContentFrame] style:UITableViewStyleGrouped];
    tv.dataSource = self;
    tv.delegate = self;
    tv.backgroundView = nil;
    tv.backgroundColor = [UIColor clearColor];
    [self.view setBackgroundColor:COLOR(239, 239, 243)];
    [self.view addSubview:tv];

    [self getData:@""];
    
}

-(void)upLevel
{
    if (flagArr.count==0) {
        
    }
    else if(flagArr.count==1)
    {
        [self getData:@""];
    }
    else
    {
        [self getData:[flagArr objectAtIndex:flagArr.count - 2]];
    }
    
    if (flagArr.count>0) {
        [flagArr removeObjectAtIndex:flagArr.count-1];
        if (flagArr.count==0) {
            self.navigationItem.rightBarButtonItem = nil;
        }
    }
}

-(void)getData:(NSString *)guid
{
    dataArray = [DBFrameService getWebInfoCategoryByParent:guid];
    [tv reloadData];
    
    [self getNums];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getNums];
}

-(void)getNums
{
    /**
    for (EpointCateGoryInfo *m in dataArray) {
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:m.CategoryGuid forKey:@"categoryguid"];
        [self startTask:@"WebInfo_GetUnreadCount" withParams:params withTaskId:1];
    }
     */
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    NSMutableArray *result = (NSMutableArray*)obj;
    if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
        for (int i=0;i<dataArray.count;i++) {
            EpointCateGoryInfo *m = [dataArray objectAtIndex:i];
            NSArray *parr = [result objectAtIndex:2];
            if ([m.CategoryGuid isEqualToString:[parr objectAtIndex:0]]) {
                m.UnreadCount = [parr objectAtIndex:1];
                [dataArray replaceObjectAtIndex:i withObject:m];
                [tv reloadData];
                break;
            }
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    EpointCateGoryInfo *m = [dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = m.CategoryName;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    int num = [m.UnreadCount intValue];
    if (num>0) {
        cell.badgeString = m.UnreadCount;
        cell.badgeColor = [UIColor colorWithRed:0.197 green:0.592 blue:0.219 alpha:1.000];
    }
    return cell;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EpointCateGoryInfo *m = [dataArray objectAtIndex:indexPath.row];
    
    NSMutableArray *temp = [DBFrameService getWebInfoCategoryByParent:m.CategoryGuid];
    
    
    if (temp.count==0)
    {
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:m.CategoryName forKey:@"title"];
        [params setObject:m.CategoryGuid forKey:@"categoryguid"];
        EpointWebInfoViewControllerCache *wvl = [[EpointWebInfoViewControllerCache alloc]init];
        wvl.passParam = params;
        [self.navigationController pushViewController:wvl animated:YES];
    }
    else{
        [flagArr addObject:m.CategoryGuid];
        [self setRightBarButtonItemWithTitle:@"上级" action:@selector(upLevel)];
        
        dataArray = temp;
        [tv reloadData];
        
    }
}


@end
